package com.star.common.sms.autoconfig;

import com.google.common.collect.Maps;
import com.star.common.sms.enums.SmsTypeEnum;
import com.star.common.sms.factory.SmsFactory;
import com.star.common.sms.properties.SmsProperties;
import com.star.common.sms.service.SmsClient;
import com.star.common.sms.service.impl.AliyunSmsClient;
import com.star.common.sms.service.impl.BoshitongSmsClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;
import java.util.Objects;

/**
 * 自动装配
 *
 * @author zhouhengzhe
 * @version 1.0
 * @date 2023/6/25 11:55
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(SmsProperties.class)
public class SmsAutoConfiguration {

    private final SmsProperties smsProperties;

    @Autowired
    public SmsAutoConfiguration(SmsProperties smsProperties) {
        this.smsProperties = smsProperties;
    }

    @Bean
    public Map<String, SmsClient> smsClientMap() {
        Map<String, SmsClient> map = Maps.newHashMap();
        if (Objects.nonNull(smsProperties.getAliyun())) {
            // 添加阿里云短信客户端
            AliyunSmsClient aliyunSmsClient = new AliyunSmsClient(smsProperties.getAliyun());
            map.put(SmsTypeEnum.ALIYUN.getCode(), aliyunSmsClient);
        }
        if (Objects.nonNull(smsProperties.getBoshitong())) {
            //添加博士通短信客户端
            BoshitongSmsClient boshitongSmsClient = new BoshitongSmsClient(smsProperties.getBoshitong());
            map.put(SmsTypeEnum.BOSHITONG.getCode(), boshitongSmsClient);
        }
        return map;
    }

    @Bean
    public SmsFactory smsService(Map<String, SmsClient> smsClientMap) {
        return new SmsFactory(smsClientMap);
    }
}
